package com.gtn.exam.title.core.initializer.impl;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.gtn.exam.title.core.exception.ModuleCommonsException;
import com.gtn.exam.title.core.initializer.PropertyInitializer;
import com.gtn.exam.title.core.util.Constants;
import com.gtn.exam.title.core.util.UtilityProperties;

/**
 * The system Properties Initializer Class, implements {@link PropertyInitializer}
 * 
 * @author Rukshan Silva | 13th August 2014
 * @version 1.0.0
 */
public class PropertyInitializerImpl implements PropertyInitializer {

	private static Logger log = Logger.getLogger(PropertyInitializerImpl.class);

	private Properties properties;
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void initializeProperties() throws ModuleCommonsException {
		
		properties = new Properties();
		
		try (
				BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(Constants.Config.SYSTEM_PROPERTY_FILE))
			) {
			
			properties.load(bufferedInputStream);
		
			/*
			 * Read properties declared in the File, one at a time. 
			 */
			UtilityProperties.initializeUtilityProperties(properties);
			
			log.info("[SUCCESS] System Properties Successfully Initialized");
		
		} catch (IOException exception) {
			
			log.error("[FAILED] Error in Initializing System Properties - " + exception.getMessage(), exception);
			exception.printStackTrace();
			
			throw new ModuleCommonsException(exception);
		}
		
		/*
		 * Does not need to add finally {} here as Java 7 Manages its Resources Automatically
		 */
	}
	
}
